<?php

namespace app\api\controller\v1\user\lottery;

use app\admin\model\DataStoreLottery;
use app\admin\model\DataUserLotteryLog;
use app\admin\model\DataUserLotteryNum;
use app\api\controller\Api;

class Number extends Api
{


    public function set(){
        $rule = [
            'code'    => 'require',
        ];
        $message  =   [
            'code.require' => '链接已失效',
        ];
        $this->validate($this->post, $rule,$message);
        $store = DataUserLotteryNum::where(['code'=>$this->post['code']])->whereNull('unid')->findOrEmpty();
        if($store->isEmpty())$this->error('链接已失效');
        $num =DataUserLotteryNum::where(['unid'=>$this->user['id'],'store_id'=>$store->store_id])->count('id');
        if(!$num+1 <3) {
            $store->unid =$this->user['id'];
            $store->save();
        }
        $log =DataUserLotteryLog::where(['unid'=>$this->user['id'],'store_id'=>$store->store_id])->count('id');
        $lottery = DataStoreLottery::where(['store_id'=>$store->store_id])->findOrEmpty();
        if($lottery->isEmpty())$this->error('获取失败');
        $lottery = $lottery->toArray();
        $lottery['times'] =$num+1-$log;
        $this->success('获取成功',$lottery);

    }

}